package com.iwhalecloud.bss.kite.cucc.service.job.scene;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.util.StopWatch;

import com.alibaba.fastjson.JSONArray;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.executor.enums.ExecutorTypeEmun;
import com.iwhalecloud.bss.kite.common.executor.task.ExecuteTask;
import com.iwhalecloud.bss.kite.common.executor.vo.ExecuteResult;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.IInitSceneOfferInstService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.cucc.service.context.CuccServiceContext;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.manager.util.KiteTransactionUtils;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

/**
 * 主商品属性还原线程类
 * @author zou.qinghua
 *
 */
public class MainOfferInstReloadExecuteTask extends ExecuteTask {

    private static final ZLogger LOGGER = ZLoggerFactory.getLogger(MainOfferInstReloadExecuteTask.class, KeyConsts.LOG_MODULE);

    private LoginInfo loginInfo;

    private List<KitePosGoodsDTO> goodsList;

    private String sceneInstId;
    private String custId;
    private String staffId;
    private String transactionId;

    public MainOfferInstReloadExecuteTask() {

    }

    public MainOfferInstReloadExecuteTask(ExecutorTypeEmun typeEmun, String taskId, String sceneInstId, String custId, String staffId, List<KitePosGoodsDTO> goodsList, LoginInfo loginInfo, String transactionId) {
        super.setTypeEmun(typeEmun);
        super.setTaskId(taskId);
        this.sceneInstId = sceneInstId;
        this.custId = custId;
        this.staffId = staffId;
        this.goodsList = goodsList;
        this.loginInfo = loginInfo;
        this.transactionId = transactionId;
    }

    @Override
    @KiteTransactional
    public ExecuteResult<String> call() throws Exception {
        KiteTransactionUtils.setTransactionId(transactionId);
        ExecuteResult<String> result = new ExecuteResult<String>();
        long time1 = System.currentTimeMillis();
        // 礼包处理
        try {
            if (Objects.isNull(ContextUtil.getLoginInfo())) {
                ContextUtil.setLoginInfo2LoginInfoThreadLocal(loginInfo);
            }
            List<String> goodsNames = this.goodsList.stream().map(KitePosGoodsDTO::getName).collect(Collectors.toList());
            List<String> goodsCodes = this.goodsList.stream().map(KitePosGoodsDTO::getCode).collect(Collectors.toList());
            StopWatch sw = new StopWatch("变更场景初始化-还原属性值-还原主商品线程");
            sw.start(JSONArray.toJSONString(goodsNames) + JSONArray.toJSONString(goodsCodes));

            CuccServiceContext.setIsCallInfForInitAttr(Boolean.FALSE);
            IInitSceneOfferInstService initSceneOfferInstService = SpringUtil.getApplicationContext().getBean(IInitSceneOfferInstService.class);
            initSceneOfferInstService.initMainOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);
            initSceneOfferInstService.initAdditionalOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);

            sw.stop();
            LOGGER.info(sw.prettyPrint());
        }
        catch(Exception e) {
            LOGGER.info(ExceptionUtils.getStackTrace(e));
            ErrorConsts.INF_SALES_HANDLE_EXCEPTION.throwOut(e.getMessage());
        }
        finally {
            //KiteTransactionUtils.commit();
            //KiteTransactionUtils.remove();
        }
        long time2 = System.currentTimeMillis();
        result.setResult(KiteStringUtils.valueOf(time2 - time1));
        return result;

    }

}
